home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / gnu / a2_0b_Emacs_sr.lha / Emacs-19.25 / lib-src / sh.c < prev    next >
C/C++ Source or Header  |  1994-08-28  |  3KB  |  147 lines

  1. ;/*
  2. SC LINK NOSTKCHK DEF SASC=1 sh.c
  3. QUIT
  4. */
  5. /*
  6.  * original version: David Gay
  7.  *
  8.  * 05/18/93 ch added SAS support, external verbose flag and primitive_parse
  9.  */
  10. #include <exec/types.h>
  11. #include <dos/dostags.h>
  12. #include <stdio.h>
  13.  
  14. #ifdef SASC
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #endif
  18.  
  19. #include <proto/dos.h>
  20.  
  21. /*
  22.   translates:
  23.  
  24.   echo "string1;string2" ; cd xx:c ; copy xx yy
  25.  
  26.       into
  27.  
  28.   echo "string1;string2" \n cd xx:c \n copy xx yy
  29.  
  30.   note:
  31.     this is a really primitive function ;-) , it may be 
  32.     changed if necessary
  33.  
  34. */
  35.  
  36. #define QUOTE    '"'
  37.  
  38. void primitive_parse(char *s)
  39. {
  40.     int c;
  41.  
  42.     while(c = *s++)
  43.     {
  44.     if(c == QUOTE)
  45.     {
  46.         while((c = *s++) && (c != QUOTE))
  47.         ;
  48.         if(!c)
  49.         break;
  50.     }
  51.     else if(c == ';')
  52.         *(s-1) = '\n';
  53.     }
  54. }
  55.  
  56. int execute(char *cmd, int debug)
  57. {
  58.     long rc;
  59.     char *s;
  60.  
  61.     while (*cmd == ' ') cmd++;
  62.     if (strncmp(cmd, "exec", 4) == 0 && (cmd[4] == ' ' || cmd[4] == '\t')) 
  63.     cmd += 4;
  64.     
  65.     s = cmd;
  66.     primitive_parse(s);
  67.  
  68.     if(debug)
  69.     fprintf(stderr,"/etc/sh: preparsed line:\n%s\n", cmd);
  70.  
  71.     if ((rc = SystemTags(cmd, SYS_UserShell, TRUE, TAG_END)) == -1)
  72.     {
  73.     fprintf(stderr, "Failed to execute command %s\n", cmd);
  74.     return 1;
  75.     }
  76.     else
  77.     return rc ? 1 : 0;
  78. }
  79.  
  80. void main(int argc, char **argv)
  81. {
  82.   int command;
  83.   char *command_string;
  84.   char *program_name = argv[0];
  85.   struct RDArgs *args;
  86.   long opts[1];
  87.   static char options[] = "";
  88.   long debug = 0;
  89.   char *shenv;
  90.  
  91.   /* Throw out AmigaDOS args so that Input() is clean */
  92.   if (args = ReadArgs(options, opts, NULL)) FreeArgs(args);
  93.  
  94.   shenv = getenv("EMACS_SH_DEBUG");
  95.  
  96.   if(shenv)
  97.       if(strstr(shenv, "-v")) /* external verbose flag */
  98.       debug = 1;
  99.   
  100.   command = 0;
  101.   /* Simplistic argument parsing */
  102.   argv++;
  103.   argc--;
  104.   while (argc > 0 && argv[0][0] == '-')
  105.     {
  106.       switch (argv[0][1])
  107.     {
  108.     case 'c':
  109.       if (argc == 1) goto usage;
  110.       command = 1;
  111.       command_string = argv[1];
  112.       argv++;
  113.       argc--;
  114.       break;
  115.     case 'v':
  116.       debug = 1;
  117.       break;
  118.     case 'i':
  119.       /* ignored for now */
  120.       break;
  121.     default: goto usage;
  122.     }
  123.       argc--;
  124.       argv++;
  125.     }
  126.   if (argc != 0) goto usage;
  127.  
  128.   if (command) 
  129.   {
  130.       if(debug)
  131.       fprintf(stderr,"%s: command_string = %s\n", argv[0], command_string);
  132.       exit(execute(command_string, debug));
  133.   }
  134.   else exit(Execute("", Input(), NULL) ? 0 : 1);
  135.  
  136.  usage:
  137.   fprintf(stderr, "%s [-i] [-v] [-c command]\n", program_name);
  138.   exit(1);
  139. }
  140.  
  141.  
  142. /*
  143.  * Local variables:
  144.  * compile-command: "sc link nostkchk def SASC=1 DEBUG=FULL sh.c"
  145.  * end:
  146.  */
  147.